home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / OrganicToolTipUI.java < prev    next >
Text File  |  1998-06-30  |  6KB  |  174 lines

  1. /*
  2.  * @(#)OrganicToolTipUI.java    1.5 98/02/23
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.organic;
  22.  
  23.  
  24. import java.awt.*;
  25. import java.awt.event.*;
  26. import com.sun.java.swing.*;
  27. import com.sun.java.swing.BorderFactory;
  28. import com.sun.java.swing.border.Border;
  29. import com.sun.java.swing.plaf.*;
  30. import com.sun.java.swing.plaf.basic.BasicToolTipUI;
  31.  
  32.  
  33. /**
  34.  * A Java L&F extension of BasicToolTipUI.
  35.  * <p>
  36.  * Warning: serialized objects of this class will not be compatible with
  37.  * future swing releases.  The current serialization support is appropriate
  38.  * for short term storage or RMI between Swing1.0 applications.  It will
  39.  * not be possible to load serialized Swing1.0 objects with future releases
  40.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  41.  * baseline for the serialized form of Swing objects.
  42.  *
  43.  * @version 1.5 02/23/98
  44.  * @author Steve Wilson
  45.  */
  46. public class OrganicToolTipUI extends BasicToolTipUI 
  47.                            implements MouseMotionListener{
  48.  
  49.     static OrganicToolTipUI sharedInstance = new OrganicToolTipUI();
  50.                  
  51.             
  52.     static JToolTip tip;
  53.     public static final int padSpaceBetweenStrings = 6;
  54.  
  55.  
  56.     public OrganicToolTipUI() {
  57.         super();
  58.     }
  59.  
  60.     public static ComponentUI createUI(JComponent c) {
  61.         return sharedInstance;
  62.     }
  63.  
  64.  
  65.     public void installUI(JComponent c) {
  66.     tip = (JToolTip)c;
  67.         super.installUI(c);
  68.     }
  69.                  
  70.     protected void installListeners(JComponent c) {
  71.       SwingUtilities.invokeLater( new Runnable() {
  72.     public void run() {
  73.         tip.getComponent().addMouseMotionListener(OrganicToolTipUI.this);
  74.     }
  75.       });
  76.     }
  77.  
  78.     protected void uninstallListener(JComponent c) {
  79.         tip.getComponent().removeMouseMotionListener(this);
  80.     }
  81.  
  82.  
  83.     public void paint(Graphics g, JComponent c) {
  84.  
  85.         FontMetrics metrics = Toolkit.getDefaultToolkit().getFontMetrics(g.getFont());
  86.         Dimension size = c.getSize();
  87.  
  88.         g.setColor(c.getBackground());
  89.         g.fillRect(0, 0, size.width, size.height);
  90.     g.setColor(OrganicLookAndFeel.getBlack());
  91.     g.drawLine(0, size.height-2, size.width, size.height-2);
  92.  
  93.     g.setColor(c.getForeground());
  94.     String tipText = ((JToolTip)c).getTipText();
  95.     String keyText = getAcceleratorString();
  96.         g.drawString(tipText, 3, 2 + metrics.getAscent());
  97.     g.setColor(OrganicLookAndFeel.getLightAccent3());
  98.     if (! (keyText.equals(""))) {  // only draw control key if there is one
  99.         g.drawString(keyText, 
  100.                  metrics.stringWidth(tipText) + 3 + padSpaceBetweenStrings, 
  101.                  2 + metrics.getAscent());
  102.     }
  103.  
  104.     }
  105.  
  106.     public Dimension getPreferredSize(JComponent c) {
  107.         FontMetrics metrics = Toolkit.getDefaultToolkit().getFontMetrics(c.getFont());
  108.  
  109.     Dimension d = new Dimension(metrics.stringWidth(((JToolTip)c).getTipText()) + 6,
  110.                              metrics.getHeight() + 4);
  111.  
  112.     String key = getAcceleratorString();
  113.     if (! (key.equals("")))
  114.         d.width += metrics.stringWidth(key) + padSpaceBetweenStrings;
  115.         return d;
  116.     }
  117.  
  118.  
  119.     /**
  120.       * implemented for MouseMotionListener.
  121.       * Move the tooltip to the right place when motion occurs
  122.       */
  123.     public void mouseMoved(MouseEvent e) {
  124.         Component source = (Component)e.getSource();
  125.     Point compLoc = (source).getLocationOnScreen();
  126.     Frame f = null;
  127.     Component parent = source;
  128.     while (f == null) {  // find the frame which encloses the given component
  129.         parent = parent.getParent();
  130.         if (parent instanceof Frame) {
  131.             f = (Frame)parent;
  132.         } // end if
  133.     } // end while
  134.     Point frameLoc = f.getLocationOnScreen();
  135.     Dimension size = tip.getSize();
  136.     final int magicMouseFudgeFactor = 20;  // this matches the number hard coded into ToolTipManager
  137.     int tipX = compLoc.x + e.getX();
  138.     int tipY = compLoc.y + e.getY()+magicMouseFudgeFactor;
  139.  
  140.         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  141.     // force tooltip onscreen
  142.         if (tipX + size.width > screenSize.width) {
  143.               tipX -= size.width;
  144.         }
  145.         if (tipY + size.height > screenSize.height) {
  146.             tipY -= (size.height + magicMouseFudgeFactor);
  147.         }
  148.     // move the tooltip to the new location
  149.     tip.getParent().setLocation(tipX, tipY);
  150.     }
  151.  
  152.     public void mouseDragged(MouseEvent e) {} // stubbed to fill MouseMotionListener requirements
  153.          
  154.     public String getAcceleratorString() {
  155.     KeyStroke[] keys = tip.getComponent().getRegisteredKeyStrokes();
  156.     String controlKeyStr = "";
  157.  
  158.     for (int i = 0; i < keys.length; i++) {
  159.  
  160.       char c = (char)keys[i].getKeyCode();
  161.       int mod = keys[i].getModifiers();
  162.       if ( mod == InputEvent.CTRL_MASK ) {
  163.           controlKeyStr = "cntl+"+(char)keys[i].getKeyCode();
  164.           break;
  165.       } else if (mod == InputEvent.ALT_MASK) {
  166.           controlKeyStr = "alt+"+(char)keys[i].getKeyCode();
  167.           break;
  168.       } 
  169.     }
  170.     return controlKeyStr;
  171.     }
  172.  
  173. }
  174.